local xiongtun = fk.CreateSkill {
  name = "tyj_heg__xiongtun",
}
local H = require "packages.ol_hegemony.util"
local U = require "packages.utility.utility"
Fk:loadTranslationTable {
  ["tyj_heg__xiongtun"] = "凶屯",
  [":tyj_heg__xiongtun"] = "结束阶段，你可令一名角色获得本回合进入弃牌堆的X张【杀】，若如此做，你获得其装备区内一张牌（X为其装备区内牌数）。",
  ["#tyj_heg__xiongtun-choose"] = "凶屯：选择 %arg 张【杀】令其获得",
  ["$tyj_heg__xiongtun1"] = "今位轻势弱，才至屯军。",
  ["$tyj_heg__xiongtun2"] = "一时弱劣，待雄厚之势必出。",
}
xiongtun:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(xiongtun.name) and target == player and player.phase == Player.Finish then
      return #table.filter(player.room.alive_players, function(p)
        return #p:getCardIds("e") > 0
      end) > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local cards = {}
    local events1 = player.room.logic:getEventsOfScope(GameEvent.MoveCards, 999, function(e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            if info.cardId and player.room:getCardArea(info.cardId) == Card.DiscardPile then
              if (Fk:getCardById(info.cardId, true).trueName == 'slash') then
                table.insertIfNeed(cards, info.cardId)
              end
            end
          end
        end
      end
    end, Player.HistoryTurn)
    if #cards > 0 then
      local tos = table.filter(room.alive_players, function(p)
        return #p:getCardIds("e") > 0 and #p:getCardIds("e") <= #cards
      end)
      if #tos > 0 then
        local to = room:askToChoosePlayers(player, {
          targets = tos,
          min_num = 1,
          max_num = 1,
          prompt = "是否发动 凶囤 令一名角色获得【杀】，然后你获得其装备区的一张牌？",
          skill_name = xiongtun.name,
        })
        if #to > 0 then
          event:setCostData(self, { to = to[1] })
          return true
        else
          return false
        end
      else
        return false
      end
    else
      return false
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = {}
    local to = event:getCostData(self).to
    if to then
      local events1 = player.room.logic:getEventsOfScope(GameEvent.MoveCards, 999, function(e)
        for _, move in ipairs(e.data) do
          if move.toArea == Card.DiscardPile then
            for _, info in ipairs(move.moveInfo) do
              if info.cardId and player.room:getCardArea(info.cardId) == Card.DiscardPile then
                if (Fk:getCardById(info.cardId, true).trueName == 'slash') then
                  table.insertIfNeed(cards, info.cardId)
                end
              end
            end
          end
        end
      end, Player.HistoryTurn)
      if #cards >= #to:getCardIds('e') then
        local get = room:askToChooseCards(player, {
          target = to,
          min = #to:getCardIds('e'),
          max = #to:getCardIds('e'),
          flag = { card_data = { { "pile_discard", cards } } },
          skill_name = xiongtun.name,
          prompt = "#tyj_heg__xiongtun-choose:::" .. tostring(#to:getCardIds('e')),
        })
        if #get > 0 then
          room:moveCards({
            ids = get,
            to = to,
            toArea = Card.PlayerHand,
            moveReason = fk.ReasonJustMove,
            proposer = player,
            skillName = xiongtun.name,
            moveVisible = true,
          })
          if player:isAlive() and to:isAlive() and #to:getCardIds("e") > 0 then
            local id = room:askToChooseCard(player, {
              target = to,
              flag = "e",
              skill_name = xiongtun.name,
              prompt = "凶屯：请获得其装备区内的一张牌"
            })
            room:obtainCard(player, id, true, fk.ReasonPrey, player, xiongtun.name)
          end
        end
      end
    end
  end,
})
return xiongtun
